<template>
  <div class="chart-size"></div>
</template>

<script>
import resize from "../../../../../components/echart/mixins/resize";

export default {
  mixins: [resize],
  props: {
    currentData: {
      type: Array,
      default: function () {
        return [];
      }
    },
    title: {
      type: String,
      default: "图标标题"
    },
    legend: {
      type: String,
      default: "line1"
    },
    color: {
      type: String,
      default: 'rgb(255, 70, 131)'
    },
    initData: {
      type: Array,
      default: () => {
        return [];
      }
    },
    areaColorInfo: {
      type: Object,
      default: () => {
        return {
          colorStopYTop: 0, // y轴正向颜色渐变
          colorStopYDown: 1 // y轴负向颜色渐变
        };
      }
    },
    loading: {
      type: Boolean,
      default: false
    },
    max: {
      type: Number,
      default: undefined,
    },
    startDay: {
      type: String,
      default: "2021-06-20"
    }
  },
  data() {
    return {
      chartData: [],
      lastTime: 0,
    };
  },
  computed: {
    option: function () {
      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            snap: true
          },
          position: function (pt) {
            return [pt[0], '10%'];
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.date
        },
        yAxis: {
          type: 'value'
        },
        dataZoom: [{
          type: 'inside',
          start: 0,
          end: 10
        }, {
          start: 0,
          end: 10,
          handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
          handleSize: '80%',
          handleStyle: {
            color: '#fff',
            shadowBlur: 3,
            shadowColor: 'rgba(0, 0, 0, 0.6)',
            shadowOffsetX: 2,
            shadowOffsetY: 2
          }
        }],
        series: [
          {
            name: '黑广播数量',
            type: 'line',
            smooth: true,
            symbol: 'none',
            sampling: 'average',
            itemStyle: {
              color: 'rgb(255, 70, 131)'
            },
            areaStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: this.areaColorInfo.colorStopYTop,
                x2: 0,
                y2: this.areaColorInfo.colorStopYDown,
                colorStops: [{
                  offset: 0, color: this.color // 0% 处的颜色
                }, {
                  offset: 1, color: 'rgba(31, 35, 38,0.1)' // 100% 处的颜色
                }],
              }
            },
            data: this.chartData
          }
        ]
      };
    },
    date: function () {
      // let base = new Date(2021, 5, 28);
      let base = new Date(this.startDay);
      let oneDay = 24 * 3600 * 1000;
      let date = [];
      let today = new Date();
      let daysdiff = Math.abs((today.getTime() - base.getTime()));
      let days = Math.ceil(daysdiff / oneDay);
      date.push(`${base.getFullYear()}/${base.getMonth() + 1}/${base.getDate()}`);
      let bases = base.getTime();
      for (let i = 1; i < days; i++) {
        let now = new Date((bases += oneDay));
        date.push([now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'));
      }
      return date.reverse();
    }
  },
  methods: {
    /**
     * 更新数据
     */
    updateData: function () {
      let option;
      if (this.max === undefined) {
        option = {
          series: [
            {
              name: this.legend,
              data: this.chartData,
              type: "line",
            },
          ],
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.date
          },
        };
      } else {
        option = {
          series: [
            {
              name: this.legend,
              data: this.chartData,
              type: "line",
            },
          ],
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.date
          },
          yAxis: {
            max: this.max
          }
        };
      }
      this.chart.setOption(option);
    },
  },
  watch: {
    /**
     * 更新当前值
     * @param val
     */
    currentData: function (val) {
      this.chartData = val;
      this.updateData();
    },
    /**
     * 初始化数据
     * @param val
     */
    initData: function (val) {
      this.chartData = val;
      this.updateData();
    },
    /**
     * 是否进入loading
     * @param val
     */
    loading: function (val) {
      if (val) {
        this.chart.showLoading();
      } else {
        this.chart.hideLoading();
      }
    }
  },
  mounted() {
    this.chartData = this.currentData;
    this.updateData();
  }
};
</script>

<style scoped>
.chart-size {
  height: 100%;
  width: 100%;
}
</style>